
package com.wyzz.hr.bo.impl;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;

import com.wyzz.hr.bo.IInfoBO;
import com.wyzz.hr.dao.IInfoDAO;
import com.wyzz.hr.pojo.Infoclass;


public class InfoBOImpl implements IInfoBO {

	public InfoBOImpl() {
		dao = null;
	}

	public List FindAllInfo() {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Infoclass.class);
		dc.addOrder(Order.asc("infoclassSortId"));
		return dao.findByCriteria(dc);
	}

	public boolean addInfo(Infoclass info) {
		String trimmedInfoclassName = info.getInfoclassName().trim();
		List old = dao.exeHqlList((new StringBuilder()).append(
				" from Infoclass  where  infoclassName = '").append(
				trimmedInfoclassName).append("'").toString());
		if (old == null || old.size() == 0) {
			info.setInfoclassName(trimmedInfoclassName);
			dao.saveObject(info);
			return true;
		}
		try {
			return false;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean delInfo(Class name, String id) {
		List check = new ArrayList();
		boolean isRefed = false;
		check = dao.exeHqlList((new StringBuilder()).append(
				"select count(*) from Information where infoClass = '").append(
				id).append("'").toString());
		if (Integer.valueOf(check.get(0).toString()).intValue() > 0)
			isRefed = true;
		Infoclass old = (Infoclass) dao.loadObject(name, id, null,
				new boolean[0]);
		if (!isRefed && old != null) {
			dao.deleteObject(old);
			return true;
		}
		try {
			return false;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean updateInfo(Infoclass info) {
		Infoclass old = (Infoclass) dao.loadObject(
				com.wyzz.hr.pojo.Infoclass.class, info.getId(), null,
				new boolean[0]);
		if (old != null) {
			String trimmedInfoclassName = info.getInfoclassName().trim();
			old.setInfoclassName(trimmedInfoclassName);
			old.setInfoclassStatus(info.getInfoclassStatus());
			dao.updateObject(old);
			return true;
		} else {
			return false;
		}
	}

	public void saveInforclassIdByBatch(String ids[]) {
		int length = ids.length;
		int sortId = 1;
		for (int i = 0; i < length; i++) {
			dao.exeHql((new StringBuilder()).append(
					"update Infoclass set infoclassSortId=").append(sortId)
					.append(" where id ='").append(ids[i]).append("'")
					.toString());
			sortId++;
		}

	}

	public IInfoDAO getDao() {
		return dao;
	}

	public void setDao(IInfoDAO dao) {
		this.dao = dao;
	}

	private IInfoDAO dao;
}
